﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.admin.crm
{
    public partial class tuwen_shucai_edit : Web.UI.ManagePage
    {
        private string action = MXEnums.ActionEnum.Add.ToString(); //操作类型

        protected string channel_name = string.Empty; //频道名称
        protected int channel_id = 16;
        private int id = 0;
        protected int categoryId = 0; 
        protected string categoryName = "";
        //页面加载事件
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = MXRequest.GetQueryString("action");
            categoryId = MyCommFun.RequestInt("category_id", -1);//图文类型：0单图文，1多图文
            this.channel_name = new BLL.channel().GetChannelName(this.channel_id); //取得频道名称
            if (!string.IsNullOrEmpty(_action) && _action == MXEnums.ActionEnum.Edit.ToString())
            {
                this.action = MXEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = MXRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.article().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {

                ChkAdminLevel("qunfa_sucai", MXEnums.ActionEnum.View.ToString()); //检查权限


                BLL.article_category cateBll = new BLL.article_category();
                this.categoryName = cateBll.GetTitle(categoryId);
                lblcategoryName.Text = this.categoryName;

                Model.wx_userweixin weixin = GetWeiXinCode();
                BLL.article bll = new BLL.article();
                int count = bll.GetCount("wid=" + weixin.id + " and  category_id=" + categoryId);

                if (action == MXEnums.ActionEnum.Add.ToString()) //新增
                {
                    if (count >= 10)
                    {
                        JscriptMsg("该多图文最多可以添加10条文章！", "back");
                        return;
                    }
                    else if (count == 0)
                    {
                        litpicCC.Text = "360*200";
                    }
                }
                else
                {
                    if (count == 1)
                    {
                        litpicCC.Text = "360*200";
                    }
                }

               

                if (action == MXEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

 


        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.article bll = new BLL.article();
            Model.article model = bll.GetModel(_id);
            
            txtTitle.Text = model.title;
            txtLinkUrl.Text = model.link_url;
            //不是相册图片就绑定
            string filename = model.img_url.Substring(model.img_url.LastIndexOf("/") + 1);
            if (!filename.StartsWith("thumb_"))
            {
                txtImgUrl.Text = model.img_url;
                imgUrl.ImageUrl = model.img_url;
            }

            txtContent.Value = model.content;
            txtSortId.Text = model.sort_id.ToString();
            txtZhaiyao.Text = model.zhaiyao.ToString();
            txtAuthor.Text = model.user_name.ToString();


        }
        #endregion



         

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.wx_userweixin weixin = GetWeiXinCode();

            BLL.article bll = new BLL.article();
            
            bool result = false;
            Model.article model = new Model.article();

            model.channel_id = this.channel_id;
            model.category_id = categoryId; 
          
            model.title = txtTitle.Text.Trim();
            model.link_url = txtLinkUrl.Text.Trim();
            model.img_url = txtImgUrl.Text;
            model.seo_title = "";
            model.seo_keywords = "";
            model.seo_description = "";
            model.wid = weixin.id;

            model.content = txtContent.Value;
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.click = 0;
            model.status = 0;
            model.is_msg = 0;
            model.is_top = 0;
            model.is_red = 0;
            model.is_hot = 0;
            model.is_slide = 0;
            model.zhaiyao = txtZhaiyao.Text.Trim();
            model.is_sys = 0;
            model.user_name = txtAuthor.Text.Trim(); //作者
            model.add_time = DateTime.Now;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            model.fields = dic; //扩展字段赋值

            if (bll.Add(model) > 0)
            {
                

                AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "图文素材添加内容:" + model.title); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.article bll = new BLL.article();
            Model.article model = bll.GetModel(_id);

            model.user_name = txtAuthor.Text.Trim(); //作者
           // model.category_id = categoryId;//待定
            model.title = txtTitle.Text.Trim();
            model.link_url = txtLinkUrl.Text.Trim();
            model.img_url = txtImgUrl.Text;
            model.seo_title = "";
            model.seo_keywords = "";
            model.seo_description = "";
            model.zhaiyao = txtZhaiyao.Text.Trim();
            model.content = txtContent.Value;
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 9);

            model.update_time = DateTime.Now;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            model.fields = dic; //扩展字段赋值

            if (bll.Update(model))
            {

                AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "图文素材修改内容:" + model.title); //记录日志
                result = true;
            }
            return result;
        }
        #endregion


        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == MXEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("qunfa_sucai", MXEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误啦！", "");
                    return;
                }
                JscriptMsg("修改图文文章成功！", "tuwen_shucai_list.aspx?id=" + categoryId);
            }
            else //添加
            {
                ChkAdminLevel("qunfa_sucai", MXEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误啦！", "");
                    return;
                }
                JscriptMsg("添加图文文章成功！", "tuwen_shucai_list.aspx?id=" + categoryId);
            }
        }
    }
}